home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / ODF Release 3 / ODFDev / ODF / OS / FWGraphx / FWGXUtil.cpp < prev    next >
Encoding:
Text File  |  1996-12-16  |  11.1 KB  |  421 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWGXUtil.cpp
  4. //    Release Version:    $ ODF 3 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWGXCFG_H
  13. #include "FWGXCfg.h"
  14. #endif
  15.  
  16. #ifdef FW_SUPPORT_GX
  17.  
  18. #ifndef FWGXUTIL_H
  19. #include "FWGXUtil.h"
  20. #endif
  21.  
  22. #ifndef FWACQUIR_H
  23. #include "FWAcquir.h"
  24. #endif
  25.  
  26. #ifndef FWDEBUG_H
  27. #include "FWDebug.h"
  28. #endif
  29.  
  30. #ifndef FWPOINT_H
  31. #include "FWPoint.h"
  32. #endif
  33.  
  34. #ifndef FWRECT_H
  35. #include "FWRect.h"
  36. #endif
  37.  
  38. // ----- Platform includes
  39.  
  40. #ifndef __GESTALT__
  41. #include <Gestalt.h>
  42. #endif
  43.  
  44. #ifndef __GXGRAPHICS__
  45. #include <GXGraphics.h>
  46. #endif
  47.  
  48. #ifndef __GXPRINTING__
  49. #include <GXPrinting.h>
  50. #endif
  51.  
  52. // ----- OpenDoc includes
  53.  
  54. #ifndef SOM_ODFrame_xh
  55. #include <Frame.xh>
  56. #endif
  57.  
  58. #ifndef SOM_ODFacet_xh
  59. #include <Facet.xh>
  60. #endif
  61.  
  62. #ifndef SOM_ODCanvas_xh
  63. #include <Canvas.xh>
  64. #endif
  65.  
  66. #ifndef SOM_ODShape_xh
  67. #include <Shape.xh>
  68. #endif
  69.  
  70. // ------ Standard includes
  71.  
  72. // #include <string.h>
  73.  
  74. //----------------------------------------------------------------------------------------
  75.  
  76. #ifdef FW_BUILD_MAC
  77. #pragma segment FW_GXUtilities
  78. #endif
  79.  
  80. //----------------------------------------------------------------------------------------
  81. // class FW_CPrivGXStatus
  82. //----------------------------------------------------------------------------------------
  83.  
  84. class FW_CPrivGXStatus
  85. {
  86. public:
  87.  
  88.     FW_CPrivGXStatus();
  89.     ~FW_CPrivGXStatus() { }
  90.     
  91.     FW_Boolean HasGXGraphics() const;
  92.     FW_Boolean HasGXPrinting() const;
  93.     
  94. protected:
  95.  
  96.     enum
  97.     {
  98.         kGXChecked = 0x01,
  99.         kGXPrinting = 0x02,
  100.         kGXGraphics = 0x04
  101.     };
  102.  
  103.     static char fgFlags;
  104. };
  105.  
  106. char FW_CPrivGXStatus::fgFlags = 0; // init the static flags
  107.  
  108. // 
  109. // FW_CPrivGXStatus::FW_CPrivGXStatus -- ctor makes sure the static flags are set
  110. //
  111. FW_CPrivGXStatus::FW_CPrivGXStatus()
  112. {
  113.     if((fgFlags & kGXChecked) == 0)
  114.     {
  115.         fgFlags |= kGXChecked;
  116.         
  117.         long version;
  118.         if(::Gestalt(gestaltGraphicsVersion, &version) == noErr)
  119.             if ((void*) ::GXEnterGraphics != (void*) kUnresolvedCFragSymbolAddress)
  120.                 fgFlags |= kGXGraphics;
  121.  
  122.         if(::Gestalt(gestaltGXPrintingMgrVersion, &version) == noErr)
  123.             if ((void*) ::GXInitPrinting != (void*) kUnresolvedCFragSymbolAddress)
  124.                 fgFlags |= kGXPrinting;
  125.     }
  126. }
  127.  
  128. //----------------------------------------------------------------------------------------
  129. // FW_CPrivGXStatus::HasGXGraphics
  130. //----------------------------------------------------------------------------------------
  131.  
  132. FW_Boolean FW_CPrivGXStatus::HasGXGraphics() const
  133. {
  134.     return (fgFlags & kGXGraphics) != 0;
  135. }
  136.  
  137. //----------------------------------------------------------------------------------------
  138. // FW_CPrivGXStatus::HasGXPrinting
  139. //----------------------------------------------------------------------------------------
  140.  
  141. FW_Boolean FW_CPrivGXStatus::HasGXPrinting() const
  142. {
  143.     return (fgFlags & kGXPrinting) != 0;
  144. }
  145.  
  146. //----------------------------------------------------------------------------------------
  147. // FW_IsGXInstalled
  148. //----------------------------------------------------------------------------------------
  149.  
  150. FW_Boolean FW_IsGXInstalled()
  151. {
  152.     return FW_CPrivGXStatus().HasGXGraphics();
  153. }
  154.  
  155. //----------------------------------------------------------------------------------------
  156. // FW_IsGXPrintingInstalled
  157. //----------------------------------------------------------------------------------------
  158.  
  159. FW_Boolean FW_IsGXPrintingInstalled()
  160. {
  161.     return FW_CPrivGXStatus().HasGXPrinting();
  162. }
  163.  
  164. //----------------------------------------------------------------------------------------
  165. // FW_GX_Initialize
  166. //----------------------------------------------------------------------------------------
  167.  
  168. void FW_Priv_GX_Initialize()
  169. {
  170.     if (FW_IsGXInstalled())
  171.     {
  172.         ::GXEnterGraphics();
  173.     }
  174.  
  175.     if (FW_IsGXPrintingInstalled())
  176.     {
  177.         ::GXInitPrinting();
  178.     }
  179. }
  180.  
  181. //----------------------------------------------------------------------------------------
  182. // FW_GX_Terminate
  183. //----------------------------------------------------------------------------------------
  184.  
  185. void FW_Priv_GX_Terminate()
  186. {
  187.     if (FW_IsGXPrintingInstalled())
  188.     {
  189.         ::GXExitPrinting();
  190.     }
  191.  
  192.     if (FW_IsGXInstalled())
  193.     {
  194.         ::GXExitGraphics();
  195.     }
  196. }
  197.  
  198. //========================================================================================
  199. // class FW_CGraphicContextGX
  200. //========================================================================================
  201.  
  202. FW_DEFINE_AUTO(FW_CGraphicContextGX)
  203.  
  204. //----------------------------------------------------------------------------------------
  205. // FW_CGraphicContextGX::FW_CGraphicContextGX
  206. //----------------------------------------------------------------------------------------
  207.  
  208. FW_CGraphicContextGX::FW_CGraphicContextGX(
  209.         Environment* ev,
  210.         ODFacet* facet,
  211.         ODShape* invalidShape)
  212. {
  213.     FW_REQUIRE(FW_IsGXInstalled());
  214.  
  215.     ODCanvas* canvas = facet->GetCanvas(ev);
  216.  
  217.     // ----- Get / create the GX objects that we will need
  218.     fGXTransform = ::GXNewTransform();
  219.     fGXViewPort = (gxViewPort) canvas->GetGXViewport(ev); 
  220.  
  221.     // ----- Set up transform mapping
  222.     SetupTransformMapping(ev, facet);
  223.  
  224.     // ----- Set up viewport clipping
  225.     SetupViewPortClipping(ev, facet, invalidShape);
  226.  
  227.     // ----- Set up transform clipping
  228.     SetupTransformClipping(ev, facet);
  229.  
  230.     // ----- Plug the viewport into the frame's transform
  231.     ::GXSetTransformViewPorts(fGXTransform, 1, &fGXViewPort);
  232.  
  233.     FW_END_CONSTRUCTOR
  234. }
  235.  
  236. //----------------------------------------------------------------------------------------
  237. // FW_CGraphicContextGX::~FW_CGraphicContextGX
  238. //----------------------------------------------------------------------------------------
  239.  
  240. FW_CGraphicContextGX::~FW_CGraphicContextGX()
  241. {
  242.     FW_START_DESTRUCTOR
  243.     
  244.     ::GXSetTransformViewPorts(fGXTransform, 0, NULL);
  245.  
  246.     RestoreTransformClipping();
  247.  
  248.     RestoreViewPortClipping();
  249.  
  250.     RestoreTransformMapping();
  251.     
  252.     ::GXDisposeTransform(fGXTransform);
  253. }
  254.  
  255. //----------------------------------------------------------------------------------------
  256. // FW_CGraphicContextGX::SetupTransformMapping
  257. //----------------------------------------------------------------------------------------
  258.  
  259. void FW_CGraphicContextGX::SetupTransformMapping(
  260.         Environment* ev,
  261.         ODFacet* facet)
  262. {
  263.     ODCanvas* canvas = facet->GetCanvas(ev);
  264.     FW_CAcquiredODTransform aqFacetXForm = facet->AcquireFrameTransform(ev, canvas);
  265.  
  266.     aqFacetXForm->GetMatrix(ev, (ODMatrix*) &fMapping);
  267.  
  268.     ::GXGetTransformMapping(fGXTransform, &fOldMapping);
  269.  
  270.     ::MapMapping(&fMapping, &fOldMapping);
  271.     ::GXSetTransformMapping(fGXTransform, &fMapping);
  272. }
  273.  
  274. //----------------------------------------------------------------------------------------
  275. // FW_CGraphicContextGX::RestoreTransformMapping
  276. //----------------------------------------------------------------------------------------
  277.  
  278. void FW_CGraphicContextGX::RestoreTransformMapping()
  279. {
  280.     ::GXSetTransformMapping(fGXTransform, &fOldMapping);
  281. }
  282.  
  283. //----------------------------------------------------------------------------------------
  284. // FW_CGraphicContextGX::SetupTransformClipping
  285. //----------------------------------------------------------------------------------------
  286.  
  287. void FW_CGraphicContextGX::SetupTransformClipping(
  288.         Environment* ev,
  289.         ODFacet* facet)
  290. {
  291.     gxShape transformClip = NULL;
  292.  
  293.     ODFrame* frame = facet->GetFrame(ev);
  294.     ODCanvas* canvas = facet->GetCanvas(ev);
  295.  
  296.     if (canvas->HasPlatformPrintJob(ev, kODQuickDrawGX))
  297.     {
  298.         // Printing
  299.         FW_CPoint extent;
  300.         frame->GetContentExtent(ev, (ODPoint*)&extent);
  301.         
  302.         FW_CRect extentRect(FW_kZeroPoint, extent);
  303.         transformClip = GXNewRectangle((gxRectangle*)&extentRect); 
  304.     }
  305.     else
  306.     {
  307.         // Drawing to the screen
  308.         FW_CAcquiredODShape aqFrameShape = frame->AcquireFrameShape(ev, NULL);
  309.         transformClip = aqFrameShape->GetGXShape(ev);
  310.     }
  311.  
  312.     fTransformClipOld = ::GXGetTransformClip(fGXTransform);
  313.     ::GXSetTransformClip(fGXTransform, transformClip);
  314.  
  315. #ifdef FW_DEBUG
  316.     gxRectangle boundsTransformClip;
  317.     ::GXGetShapeBounds(transformClip, 0, &boundsTransformClip);
  318.  
  319.     gxRectangle boundsTransformClipOld;
  320.     ::GXGetShapeBounds(fTransformClipOld, 0, &boundsTransformClipOld);
  321. #endif
  322.  
  323.     ::GXDisposeShape(transformClip);
  324. }
  325.  
  326. //----------------------------------------------------------------------------------------
  327. // FW_CGraphicContextGX::RestoreTransformClipping
  328. //----------------------------------------------------------------------------------------
  329.  
  330. void FW_CGraphicContextGX::RestoreTransformClipping()
  331. {
  332.     ::GXSetTransformClip(fGXTransform, fTransformClipOld);
  333.     ::GXDisposeShape(fTransformClipOld);
  334. }
  335.  
  336. //----------------------------------------------------------------------------------------
  337. // FW_CGraphicContextGX::SetupViewPortClipping
  338. //----------------------------------------------------------------------------------------
  339.  
  340. void FW_CGraphicContextGX::SetupViewPortClipping(
  341.         Environment* ev,
  342.         ODFacet* facet,
  343.         ODShape* invalidShape)
  344. {
  345.     ODCanvas* canvas = facet->GetCanvas(ev);
  346.     FW_CAcquiredODShape aqFacetClipShape = facet->AcquireAggregateClipShape(ev, canvas);
  347.  
  348. #ifdef FW_DEBUG
  349.     FW_CRect boundsFacetClipShape;
  350.     aqFacetClipShape->GetBoundingBox(ev, (ODRect*) &boundsFacetClipShape);
  351. #endif
  352.  
  353.     // ----- Map the clip shape's copy back to local coordinates
  354.     gxShape    facetGXShape = aqFacetClipShape->GetGXShape(ev);
  355.     gxShape    viewPortClip = ::GXCopyToShape(NULL, facetGXShape);
  356.     ::GXDisposeShape(facetGXShape);
  357.  
  358.     ::GXSetShapeAttributes(viewPortClip, GXGetShapeAttributes(viewPortClip) & ~gxMapTransformShape);
  359. //    ::GXMapShape(viewPortClip, &fMapping);
  360.  
  361.     // ----- Intersect the clip shape with the invalid  shape
  362.     if (invalidShape != NULL)
  363.     {
  364.         gxShape invalidGXShape = invalidShape->GetGXShape(ev);
  365.         ::GXIntersectShape(viewPortClip, invalidGXShape);
  366.         ::GXDisposeShape(invalidGXShape);
  367.     }
  368.     
  369.     //    [HLX] move here because of bug #1306478
  370.     ::GXMapShape(viewPortClip, &fMapping);
  371.  
  372.     // ----- Save the current clip and set the new old 
  373.     fViewPortClipOld = ::GXGetViewPortClip(fGXViewPort);
  374.  
  375. #ifdef FW_DEBUG
  376.     gxRectangle boundsViewPortClip;
  377.     ::GXGetShapeBounds(viewPortClip, 0, &boundsViewPortClip);
  378.  
  379.     gxRectangle boundsViewPortClipOld;
  380.     ::GXGetShapeBounds(fViewPortClipOld, 0, &boundsViewPortClipOld);
  381. #endif
  382.  
  383.     ::GXSetViewPortClip(fGXViewPort, viewPortClip);
  384.     ::GXDisposeShape(viewPortClip);
  385. }
  386.  
  387. //----------------------------------------------------------------------------------------
  388. // FW_CGraphicContextGX::RestoreViewPortClipping
  389. //----------------------------------------------------------------------------------------
  390.  
  391. void FW_CGraphicContextGX::RestoreViewPortClipping()
  392. {
  393.     ::GXSetViewPortClip(fGXViewPort, fViewPortClipOld);
  394.     ::GXDisposeShape(fViewPortClipOld);
  395. }
  396.  
  397. //----------------------------------------------------------------------------------------
  398. // FW_CGraphicContextGX::DrawShape
  399. //----------------------------------------------------------------------------------------
  400.  
  401. void FW_CGraphicContextGX::DrawShape(gxShape shape)
  402. {
  403.     gxTransform oldTransform = ::GXGetShapeTransform(shape);
  404.     ::GXSetShapeTransform(shape, fGXTransform);
  405.     
  406.     ::GXDrawShape(shape);
  407.  
  408.     ::GXSetShapeTransform(shape, oldTransform);
  409. }
  410.  
  411. //----------------------------------------------------------------------------------------
  412. // FW_CGraphicContextGX::GetGXTransform
  413. //----------------------------------------------------------------------------------------
  414.  
  415. gxTransform FW_CGraphicContextGX::GetGXTransform() const
  416. {
  417.     return fGXTransform;
  418. }
  419.  
  420. #endif    // FW_SUPPORT_GX
  421.